// /////////////////////////////////////////////////////////////////////////////
//
// Name:            TerrainComponent.h
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           Declaration of the TerrainComponent class.
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

#ifndef __TERRAINCOMPONENT_H__
#define __TERRAINCOMPONENT_H__

// Include files
#include "../../dependencies.h"
#include "SceneComponent.h"


// TerrainComponent class
//! Component wrapper of Irrlicht's ITerrainSceneNode.
//!
//! <b>XML:</b>
//! \code
//! <TerrainComponent>
//!    <!-- Properties -->
//! </TerrainComponent>
//! \endcode
class TerrainComponent : public SceneComponent
{
public:

    // Initialisation and deinitialisation
    //! Constructor (default)
    //! @param parent        The parent entity to which the component should be added.
    TerrainComponent(Entity *parent);
    //! Constructor
    //! @param parent        The parent entity to which the component should be added.
    //! @param fileName      Filename of the heightmap to load.
    //! @param scale         The initial scale of the component.
    //! @param vertexColor   The default color of all the vertices.
    //! @param maxLOD        The maximum LOD (level of detail) for the component.
    //! @param smoothFactor  The number of times the vertices are smoothed.
    TerrainComponent(Entity *parent, const std::string &fileName,
                     const vector3df &scale = vector3df(1.0f, 1.0f, 1.0f),
                     const SColor &vertexColor = SColor(255, 255, 255, 255), s32 maxLOD = 5,
                     s32 smoothFactor = 0);
    //! Deconstructor
    ~TerrainComponent();

    // Methods
    //! Returns a direct pointer to the ITerrainSceneNode.
    //! @note Not available in AngelScript.
    ITerrainSceneNode* getTerrainSceneNode();

    //! Get the bounding box of the terrain.
    const aabbox3df& getBoundingBox() const;
    //! Gets the bounding box of a patch.
    const aabbox3df& getBoundingBox(s32 patchX, s32 patchZ) const;
    //! Gets height of a point of the terrain.
    f32 getHeight(f32 x, f32 y);
    //! Returns the current mesh.
    //! @note Not available in AngelScript.
    IMesh* getMesh();
    //! Gets the center of the terrain.
    const vector3df& getTerrainCenter() const;

    //! Loads the data from a heightMapFile.
    //!
    //! <b>XML:</b>
    //! \code
    //! <heightMap type="string" value="" />
    //! \endcode
    //!
    //! @param fileName      Filename of the heightmap to load.
    //! @param vertexColor   The default color of all the vertices.
    //! @param smoothFactor  The number of times the vertices are smoothed.
    bool loadHeightMap(const std::string &fileName, const SColor &vertexColor =
                       SColor(255, 255, 255, 255), s32 smoothFactor = 0);

    //! Scales the base texture.
    //! @param scale         The scaling amount. Values above 1.0 increase the number of time
    //!                      the texture is drawn on the terrain. Values below 0 will decrease
    //!                      the number of times the texture is drawn on the terrain. Using
    //!                      negative values will flip the texture, as well as still scaling it.
    //! @param scale2        If set to 0 (default value), this will set the second texture
    //!                      coordinate set to the same values as in the first set. If this is
    //!                      another value than zero, it will scale the second texture
    //!                      coordinate set by this value.
    void scaleTexture(f32 scale = 1.0f, f32 scale2 = 0.0f);

    // Events
    //! Responds to changes of the heightmap attached to this component.
    //! @note For internal use only!
    void onHeightMap(void *p);

    // Serialization
    //! Serializes this object.
    //! @param attributes
    virtual bool onSerialization(SerializedAttributes *attributes);
    //! Deserializes this object.
    //! @param attributes
    virtual bool onDeserialization(SerializedAttributes *attributes);

private:

    // Private methods
    //! Common initialisation method.
    void init(const vector3df &scale = vector3df(1.0f, 1.0f, 1.0f),
              const SColor &vertexColor = SColor(255, 255, 255, 255),
              s32 maxLOD = 5, s32 smoothFactor = 0);

    // Private members
    ITerrainSceneNode *mTerrainSN;

    std::string mHeightMapFileName;
    SColor mVertexColor;
    s32 mSmoothFactor;
};

#endif
